import graphics, os, math, pygame
from gameObject import GameObject

class Explosion( GameObject ):
    """
    Animation for various explosions.
    """
    FRAME_TIME = 0.03 # In seconds.

    def __init__(self, x, y, angle, time, game, kind="tank"):
        # First initiate GameObject since this is a new __init__.
        GameObject.__init__(self)
        self.x = x
        self.y = y
        self.angle = - angle
        self.timeUpdated = time
        self.currentFrame = 0
        self.game = game
        # Load in all of the sprites.
        self.sprites = [pygame.transform.rotate(s, math.degrees(self.angle)) for s in graphics.loadSprites("sprites/" + kind + "Explosion/")]
        self.image = self.sprites[self.currentFrame]
        self.rect = self.image.get_rect()
        self.rect.center = (self.x, self.y)

    def update(self, previousTime, currentTime):
        dt = currentTime - self.timeUpdated
        if dt >= Explosion.FRAME_TIME:
            self.currentFrame += 1
            self.timeUpdated = currentTime
            if self.currentFrame <= len(self.sprites) - 1:
                self.image = self.sprites[self.currentFrame]
            else:
                self.game.explosions.remove(self)
            
    def clear(self):
        self.game.display.blit(self.game.background, self.rect, self.rect)

    def draw(self):
        self.game.display.blit(self.image, self.rect)
            
            
